package lxh.imitate58loadingview.loadingview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

import lxh.imitate58loadingview.R;

/**
 * Created by liuxh on 2017/7/29.
 */

public class LoadingView extends LinearLayout {
    private ShapeView mShapeView;
    private View mShadowView;

    private float mTranslationDistance;//下落距离
    private static long DURATION_TIME=500;//动画时间

    private boolean mIsStopAnimation=false;
    public LoadingView(Context context) {
        this(context,null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTranslationDistance=dipToPx(80);
        initLayout();
    }

    private float dipToPx(int i) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,i, getResources().getDisplayMetrics());
    }

    /**
     * 初始化布局
     */
    private void initLayout() {
        inflate(getContext(), R.layout.custom_loading_view,this);
        mShapeView= (ShapeView) findViewById(R.id.shapeView);
        mShadowView=findViewById(R.id.shadowView);

        post(new Runnable() {
            @Override
            public void run() {
                //下落动画
                startFallAnimation();//让动画在onResume()之后展示
            }
        });


    }

    /**
     * 下落动画
     */
    private void startFallAnimation() {
        if (mIsStopAnimation)return;
        //移动
        ObjectAnimator translationAnimator=ObjectAnimator.ofFloat(mShapeView,"translationY",0,mTranslationDistance);
        //缩放,下落过程中阴影慢慢变大
        ObjectAnimator scaleAnimation=ObjectAnimator.ofFloat(mShadowView,"scaleX",0.3f,1.0f);

        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.setDuration(DURATION_TIME);
        animatorSet.setInterpolator(new AccelerateInterpolator());
        animatorSet.playTogether(translationAnimator,scaleAnimation);
        animatorSet.start();

        //对下落动画监听
        translationAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //下落动画结束后，启动弹起动画
                startUpAnimation();
            }
        });
    }

    /**
     * 弹起动画
     */
    private void startUpAnimation() {
        if (mIsStopAnimation)return;
        //移动
        ObjectAnimator translationAnimator=ObjectAnimator.ofFloat(mShapeView,"translationY",mTranslationDistance,0);
        //缩放,下落过程中阴影慢慢变大
        ObjectAnimator scaleAnimation=ObjectAnimator.ofFloat(mShadowView,"scaleX",1.0f,0.3f);

        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.setDuration(DURATION_TIME);
        animatorSet.playTogether(translationAnimator,scaleAnimation);
        animatorSet.setInterpolator(new DecelerateInterpolator());
        animatorSet.start();
        //对弹起动画监听
        translationAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //弹起动画结束后，启动下落动画
                startFallAnimation();
                //形状变换
                mShapeView.exchange();
                //形状旋转动画效果
                startRotationAnimation();
            }
        });
    }

    /**
     * 图形旋转动画
     */
    private void startRotationAnimation() {
        ShapeView.Shape currentShape = mShapeView.getCurrentShape();
        ObjectAnimator rotationAnimation=null;
        switch (currentShape){
            //180
            case Circle:
            case Square:
                rotationAnimation=ObjectAnimator.ofFloat(mShapeView,"rotation",180,0);
                break;
            case Triangle:
                //120
                rotationAnimation=ObjectAnimator.ofFloat(mShapeView,"rotation",120,0);
                break;
        }
        rotationAnimation.setDuration(DURATION_TIME);
        rotationAnimation.setInterpolator(new DecelerateInterpolator());
        rotationAnimation.start();
    }

    /**
     * 控件不可见时优化操作
     * @param visibility
     */
    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(INVISIBLE);//省去重新测量布局操作
        //清理动画
        mShapeView.clearAnimation();
        mShadowView.clearAnimation();
        //移动LoadingView
        ViewGroup parent = (ViewGroup) getParent();
        if (parent!=null){
            parent.removeView(this);
            removeAllViews();
        }
        mIsStopAnimation=true;
    }
}
